package com.itheima.homenwork.test01;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class demo04 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("books.xml"));
        Element rootElement = document.getRootElement();
        List<Element> list = rootElement.elements();
        for (Element element : list) {
            Attribute idEle = element.attribute("id");
            String id = idEle.getValue();
            System.out.println(id);

            Element nameEle = element.element("name");
            String name = nameEle.getText();

            Element publishEle = element.element("publish");
            String publish = publishEle.getText();

            Element dateEle = element.element("date");
            String date = dateEle.getText();
            Element priceEle = element.element("price");
            String price = priceEle.getText();

            System.out.printf("id=%s 书名=%s 出版社=%s 出版日期=%s 价格=%s ",
                    id,name,publish,date,price);
        }
    }
}
